import {watch, unref} from 'vue';
// import { useI18n } from '/@/hooks/web/useI18n';
import {useTitle as usePageTitle} from '@vueuse/core';
import {useGlobSetting} from '/@/hooks/setting';
import {useRouter} from 'vue-router';

import {REDIRECT_NAME} from '/@/router/constant';

/**
 * 根据路由信息动态修改标题
 */
export function useTitle(): void {
    const {title} = useGlobSetting();
    // const { t } = useI18n();
    const {currentRoute} = useRouter();

    const pageTitle = usePageTitle();

    watch(
        () => currentRoute.value.path,
        () => {
            const route = unref(currentRoute);
            if (route.name === REDIRECT_NAME) {
                return;
            }

            const tTitle = route?.meta?.title as string;
            pageTitle.value = tTitle ? ` ${tTitle} - ${title} ` : `${title}`;
        },
        {immediate: true}
    );
}
